package ui.popup 
{
	import events.GCEvent;
	import ui.button.GCButton;

	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCConfirmationPopup extends Sprite
	{
		private var _textArea:TextField;
		private var _confirmButton:GCButton;
		private var _cancelButton:GCButton;
		private var _callback:Function;
		
		/**
		 * Constructor
		 */
		public function GCConfirmationPopup(callback:Function) 
		{
			// UI components			
			_textArea = new TextField();			
			_confirmButton = new GCButton(onClickConfirm);
			_cancelButton = new GCButton(onClickCancel);
			
			// The function that gets called on confirm
			_callback = callback;
			
			init();
		}
		
		private function init():void
		{
			// Message
			_textArea.wordWrap = true;
			this.addChild(_textArea);			
			
			// Define the UI component
			_confirmButton.loadGraphic(GCResources.plate, 100, 30);
			_confirmButton.loadText("Confirm");
			
			_cancelButton.loadGraphic(GCResources.plate, 100, 30);
			_cancelButton.loadText("Cancel");
			
			// Add the components
			this.addChild(_confirmButton);
			this.addChild(_cancelButton);
		}
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTION
		//----------------------------------------------------------------------		
		
		public function initializePopup(message:String):void
		{
			_textArea.width = 230;
			_textArea.text = message;
			
			_confirmButton.x = 10;
			_confirmButton.y = this.height - _confirmButton.height;
			
			_cancelButton.x = _confirmButton.x + _confirmButton.width + 10;
			_cancelButton.y = _confirmButton.y;
			
			//Popup background
			var back:Sprite = GCResources.getMaskedCanvasBackground(this.width, this.height);
			this.addChildAt(back, 0);
		}
		
		public function clearPopup():void
		{
			_textArea.text = "";
		}
		
		//----------------------------------------------------------------------
		//	PRIVATE FUNCTION
		//----------------------------------------------------------------------		
		
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------		
		
		private function onClickConfirm(event:MouseEvent):void
		{
			// Clean up the popup
			onExitCleanup();
			
			if (_callback != null)
				_callback(event);
		}
		
		private function onClickCancel(event:MouseEvent):void
		{
			// Clean up the popup
			onExitCleanup();
			
			dispatchEvent(new GCEvent(GCEvent.CANCEL_EVENT));
		}
		
		private function onExitCleanup():void
		{
			// Disable the buttons
			_cancelButton.disable();
			_confirmButton.disable();
		}
		
	}
}